﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using Volo.Abp.Settings;

namespace Qianke.Core.Common
{
    public class AsyncEnvironment
    {
        private static AsyncLocal<Dictionary<string, object>> ExtraData { get; } = new AsyncLocal<Dictionary<string, object>>();


        public static void SetData(string key, object value)
        {
            if (ExtraData.Value == null)
                ExtraData.Value = new Dictionary<string, object>();
            ExtraData.Value[key] = value;
        }

        public static object GetData(string key, object defaultValue = default)
        {
            if (ExtraData.Value == null)
                return defaultValue;
            return ExtraData.Value.ContainsKey(key) ? ExtraData.Value[key] : defaultValue;
        }

        public static TValue GetData<TValue>(string key, TValue defaultValue = default)
        {
            return (TValue)GetData(key, defaultValue);
        }

        public static Dictionary<string, object> GetAllData() => ExtraData.Value;
    }
}
